package com.tencent.cloud.uikit.widget.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.tencent.cloud.iov.util.R;


/**
 * 两端对齐的 TextView
 *
 * @author vinsonhuang
 * @date 2018/6/26
 */
public class JustifyTextView extends AppCompatTextView {

    private static final int SUFFIX_MODE_FULL_WIDTH = 1;
    private static final int SUFFIX_MODE_HALF_WIDTH = 2;

    /**
     * 文字
     */
    private String mText;

    /**
     * 后缀符号全角或半角
     */
    private int mSuffixMode;

    /**
     * 后缀
     */
    private String mSuffixStr;

    private Paint mPaint;

    private Rect mTargetRect;

    public JustifyTextView(Context context) {
        this(context, null);
    }

    public JustifyTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JustifyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlignTextView);
        mText = typedArray.getString(R.styleable.AlignTextView_text);
        mSuffixMode = typedArray.getInt(R.styleable.AlignTextView_suffixMode, 0);
        typedArray.recycle();

        mPaint = new Paint();
        mTargetRect = new Rect(0, 0, getWidth(), getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(getTextSize());
        mPaint.setColor(getTextColors().getDefaultColor());
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        int baseline = (mTargetRect.bottom + mTargetRect.top
                - fontMetrics.bottom - fontMetrics.top) / 2;
        //下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()问题宽度
        float textPx = getTextSize();
        // TextView控件总宽度
        int maxTextViewWidth = getWidth();

        if (TextUtils.isEmpty(mText)) {
            super.onDraw(canvas);
            return;
        }

        //情况一:无后缀
        if (mSuffixMode == 0) {
            char[] chars = mText.toCharArray();
            int spec = (int) ((maxTextViewWidth - chars.length * textPx) / (chars.length - 1));
            for (int i = 0; i < chars.length; i++) {
                canvas.drawText(String.valueOf(chars[i]), i * (textPx + spec), baseline, mPaint);
            }
            //情况二:有后缀
        } else {
            //后缀字符
            mSuffixStr = mText.substring(mText.length() - 1, mText.length());
            //后缀宽度
            int suffixWidth = getSuffixWidth(mPaint, mSuffixStr, mSuffixMode);
            char[] chars = mText.substring(0, mText.length() - 1).toCharArray();
            int spec = (int) ((maxTextViewWidth - chars.length * textPx - suffixWidth)
                    / (chars.length - 1));
            for (int i = 0; i < chars.length; i++) {
                canvas.drawText(String.valueOf(chars[i]), i * (textPx + spec), baseline, mPaint);
            }
            canvas.drawText(mSuffixStr, maxTextViewWidth - suffixWidth, baseline, mPaint);
        }
        super.onDraw(canvas);
    }

    /**
     * 计算后缀的宽度
     *
     * @param paint
     * @param suffixStr
     * @param suffixMode
     * @return
     */
    private int getSuffixWidth(Paint paint, String suffixStr, int suffixMode) {
        if (TextUtils.isEmpty(suffixStr) || suffixMode == 0) {
            return 0;
        }
        Rect rect = new Rect();
        switch (suffixMode) {
            case SUFFIX_MODE_FULL_WIDTH:
                //计算出一个全角字符的宽度
                paint.getTextBounds("啊", 0, 1, rect);
                break;

            case SUFFIX_MODE_HALF_WIDTH:
                //计算出一个半角字符的宽度
                paint.getTextBounds("a", 0, 1, rect);
                break;

            default:
                break;
        }
        return rect.width();
    }

    public void setAlingText(String text) {
        this.mText = text;
        invalidate();
    }

}

